# $Id: Fiche.pm,v 1.2 2004/06/21 12:12:30 ibo Exp $
package CGI::FOGE2::Fiche;

use strict;
use CGI::FOGE2::Tools;

our @ISA = qw();

sub new {
    my $proto = shift;
    my $class = ref($proto) || $proto;
    
    my %args = @_;
    my @mandatory_keys = ();
    my $self = {
	form => $args{form},
	id => $args{id},
    };
    bless ($self, $class);
    return $self;
}


sub serve {
    my $self = shift;
    my $form = $self->{form};
    my $view = $form->{view};
    my $myself = CGI::script_name();
    my $db = $form->dbh_r();

    my $query = "SELECT %s FROM %s WHERE id=$self->{id};\n";

    my $out = '';
    foreach my $sec (@{$form->{sections}}) {
	my $secname = $sec->get_table_name();
	next unless $view->contains_section($secname);
	#my $has_prolog = defined $sec->{prolog};
	#my $has_epilog = defined $sec->{epilog};
	
	$out .= CGI::h2({-class => "sectitle"}, $sec->{title});
	#$out .= CGI::span({-class => "sechead"},  $sec->{prolog}) if $has_prolog;
	
	$out .= CGI::start_table({-width => '100%'});
	foreach my $question (@{$sec->{questions}}) {
	    my $qname = $question->generate_field_names()->[0];
	    next unless $view->contains_question($secname, $qname);	    
	    my $q = sprintf $query, $secname. '.' . $qname, $secname;
	    my @r = $db->selectrow_array($q);
	    my $val = $r[0];
	    $out .= table3cols
		( $question->{title},
		  $question->{ismandatory},
		  $val);
	}
	$out .= CGI::end_table();	
	#$out .= CGI::span({-class => "sechead"}, $sec->{epilog}) if $has_epilog;
    }
    return $out;
}
1;
